package com.jiyu.controller;

import com.jiyu.entity.User;
import com.jiyu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Random;

@Slf4j
@Controller
public class LoginController {

    @Autowired
    UserService userService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody User requestUser){
//        System.out.println(requestUser);

        int account = requestUser.getAccount();
        String password = requestUser.getPassword();
        User user = userService.get(account, password);
        System.out.println("user: "+user);
        if(user!=null){
            return "200";
        }

        return "账号或密码错误！";
    }

    //登录成功后跳转到聊天室，发送给前端用户名
    @RequestMapping("/toChat")
    public String toChat(@RequestParam int account, @RequestParam String password, Model model){
        User user = userService.get(account, password);
        model.addAttribute("username", user.getUsername());
        return "chatroom";
    }

    //注册时对账号和用户名进行检测
    @RequestMapping("/register/check")
    @ResponseBody
    public String registerCheck(@RequestBody User requestUser){
        int account = requestUser.getAccount();
        String password = requestUser.getPassword();
        String username = requestUser.getUsername();

        String res="";
        int flag=0;

        if(userService.isExist(username)){
            res+="昵称重复！";
            flag=1;
        }
        if(userService.accountIsExist(account)){
            res+="账号重复！";
            flag=1;
        }
        if(flag==0){
            return "200";
        }else{
            return res;
        }
    }

    //注册时对账号和用户名进行检测
    @RequestMapping("/register")
    @ResponseBody
    public String register(@RequestBody User requestUser){
//        System.out.println(requestUser);

        int account = requestUser.getAccount();
        String password = requestUser.getPassword();
        String username = requestUser.getUsername();
        Random rand = new Random();
        int level=rand.nextInt(100)+1;

        userService.add(new User(account,username, password,"avatar",level));

        return "200";
    }

    //注册成功则跳转到登录页
    @RequestMapping("/toLogin")
    public String toLogin(@RequestParam("avatar") MultipartFile file, String username, int account,String password){

        //处理图片
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();

        System.out.println(("上传的文件名为：" + fileName));
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println(("上传的后缀名为：" + suffixName));

        //修改上传完成后的文件名(没有该需求可以删除这行)
        fileName=account+suffixName;
        // 文件上传后的路径
        String filePath = "D://test//chatroom//";
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            System.out.println("上传成功");
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //上传到数据库
        String avatar="/assets/"+fileName;
        Random rand = new Random();
        int level=rand.nextInt(100)+1;
        userService.add(new User(account,username, password,avatar,level));

        return "login";
    }
}
